package novel.spider.impl;

import novel.spider.entitys.Chapter;
import novel.spider.interfaces.IChapterSpider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建一个类实现序列化文章接口
 * Created by Kau on 2016/10/30.
 */
public class AbstractChapterSpider implements IChapterSpider {

    protected String crawl(String url) throws Exception{
        /**
         * 创建一个HttpClient实例并接收服务器返回的各种头信息和正文
         */
        try (CloseableHttpClient httpClient = HttpClientBuilder.create().build();CloseableHttpResponse httpResponse = httpClient.execute(new HttpGet(url))){
            String result = EntityUtils.toString(httpResponse.getEntity());
            return result;

        } catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public List<Chapter> getsChapter(String url) {
        try {
            String result = crawl(url);
            Document doc = Jsoup.parse(result);
            //使用jsoup的css选择器将html标签获取
            Elements as = doc.select("#list dd a");
            List<Chapter> chapters = new ArrayList<>();
            /**
             * 将获取到的标签和内容使用ArrayList存储
             */
            for (Element a : as){
                Chapter chapter = new Chapter();
                chapter.setTitle(a.text());
                chapter.setUrl("http://www.biquge.tw/" + a.attr("href"));
                chapters.add(chapter);
            }
            return chapters;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
